package com.team7.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.team7.common.entity.CollectionTeacher;
import com.team7.service.CollectionTeacherService;
import com.team7.common.vo.CommonResult;
import com.team7.dao.CollectionTeacherDao;

import com.team7.vo.AttentionUtil;
import com.team7.vo.AttentionVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: team7-Object02
 * @description: 实现类
 * @author: 王子鑫
 * @create: 2021-10-21 21:22
 **/
@Service
public class CollectionTeacherServiceImpl implements CollectionTeacherService {
    @Resource
    private CollectionTeacherDao collectionTeacherDao;

    @Override
    public CommonResult findFavoriteTeacher(AttentionVo attentionVo) {
        QueryWrapper<CollectionTeacher> wrapper = new QueryWrapper<>();
        wrapper.like("member_id",attentionVo.getMemberid());
        wrapper.like("teacher_id",attentionVo.getTeacherid());
        CollectionTeacher teacher = collectionTeacherDao.selectOne(wrapper);
        if (teacher!=null){
            attentionVo.setStatus(1);
            return new CommonResult(2000,"查询成功",attentionVo);
        }else {
            attentionVo.setStatus(0);
            return new CommonResult(2000,"查询成功",attentionVo);
        }
    }

    @Override
    public CommonResult findFavoriteTeacherById(String memberId) {
        List<AttentionUtil> teacher = collectionTeacherDao.findFavoriteTeacherById(memberId);
        if (teacher.size()>0){
            return new CommonResult(2000,"查询成功",teacher);
        }else {
            return new CommonResult(5000,"暂无收藏",teacher);
        }
    }

    @Override
    public CommonResult addFavoriteTeacher(AttentionVo attentionVo) {
        QueryWrapper<CollectionTeacher> wrapper = new QueryWrapper<>();
        wrapper.eq("member_id",attentionVo.getMemberid());
        wrapper.eq("teacher_id",attentionVo.getTeacherid());
        CollectionTeacher teacher = collectionTeacherDao.selectOne(wrapper);
        if (teacher==null){
            CollectionTeacher collectionTeacher = new CollectionTeacher();
            collectionTeacher.setTeacherId(attentionVo.getTeacherid());
            collectionTeacher.setMemberId(attentionVo.getMemberid());
            int i = collectionTeacherDao.insert(collectionTeacher);
            if (i>0){
                return new CommonResult(2000,"添加成功",null);
            }else {
                return new CommonResult(5000,"添加失败",null);
            }
        }
        return new CommonResult(5000,"已经添加过了",null);
    }

    @Override
    public CommonResult deleteFavoriteTeacher(AttentionVo attentionVo) {
        System.out.println(attentionVo+"2222222222222222");
        QueryWrapper<CollectionTeacher> wrapper = new QueryWrapper<>();
        wrapper.eq("member_id",attentionVo.getMemberid());
        wrapper.eq("teacher_id",attentionVo.getTeacherid());
        int i = collectionTeacherDao.delete(wrapper);
        System.out.println(i+"33333333333333333");
        if (i>0){
            return new CommonResult(2000,"取关成功",null);
        }else {
            return new CommonResult(5000,"取关失败",null);
        }
    }
}
